package com.baibei.ebec.user.message;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.RaeTabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.baibei.basic.IPresenter;
import com.baibei.ebec.user.R;
import com.baibei.ebec.user.R2;
import com.baibei.ebec.user.messagefollow.MessageFollowFragment;
import com.baibei.ebec.user.messagenotice.SystemMessageNoticeFragment;
import com.baibei.ebec.user.systemmessage.SystemMessageFragment;
import com.baibei.ebec.user.ticket.TicketContract;
import com.baibei.model.CouponInfo;
import com.baibei.module.AppRouter;
import com.baibei.module.BasicActivity;
import com.baibei.ui.AppUI;
import com.baibei.widget.CommonRefreshLayout;
import com.rae.widget.RaePlaceHolderLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import in.srain.cube.views.ptr.PtrDefaultHandler;
import in.srain.cube.views.ptr.PtrFrameLayout;

/**
 * 消息中心
 * Created by ChenRui on 2017/8/21 0021 17:51.
 */
@Route(path = AppRouter.ROUTER_MESSAGE)
public class MessageActivity extends BasicActivity {


    @BindView(R2.id.tab_layout)
    RaeTabLayout mTablayout;
    @BindView(R2.id.view_pager)
    ViewPager mViewPager;
//   private SystemMessageFragment mSystemmessageFragment;
    /**
     * 标题
     */
    private String[] mTitles;

    /**
     * 订货记录
     * 收支记录
     * 取货记录
     */
    private List<Fragment> mFragmentList;
    FragmentAdapter mAdapter;
    private String extrakey;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_message);
        ButterKnife.bind(this);
        initView();
    }

    private void initView() {
        mTitles = new String[]{
                "跟投消息",
                "系统消息","行情提醒"};
        for (String title : mTitles) {
            mTablayout.addTab(mTablayout.newTab().setText(title));
        }
        mFragmentList = new ArrayList<>();

        mFragmentList.add(new MessageFollowFragment());
        mFragmentList.add(new SystemMessageFragment());
        mFragmentList.add(new SystemMessageNoticeFragment());
        mAdapter = new FragmentAdapter(getSupportFragmentManager());
        mViewPager.setOffscreenPageLimit(mFragmentList.size());
        mViewPager.setAdapter(mAdapter);
        mTablayout.setupWithViewPager(mViewPager);
        Bundle bundle = getIntent().getExtras();
        String extrakey = bundle.getString("extrakey");
        jumpByKey(extrakey);
    }

    private void jumpByKey(String extrakey) {
//        Bundle bundle = getIntent().getExtras();
//        extrakey = bundle.getString("extrakey");
        if (extrakey == null) return;
        switch (extrakey){
            case "行情提醒":
                mViewPager.setCurrentItem(2);
                break;
            case "系统消息":
                mViewPager.setCurrentItem(1);
                break;
            case "跟投消息":
                mViewPager.setCurrentItem(0);
                break;
        }
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        String extrakey = intent.getExtras().getString("extrakey");
        jumpByKey(extrakey);
    }

    @Override
    public IPresenter getBasicPresenter() {
        return null;
    }

    private class FragmentAdapter extends FragmentPagerAdapter {

        FragmentAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return mFragmentList.get(position);
        }

        @Override
        public int getCount() {
            return mFragmentList.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return mTitles[position];
        }
    }
}
